package com.twotiger.and.activity.base;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import app.twotiger.p2p.R;

import com.readystatesoftware.systembartint.SystemBarTintManager;
import com.twotiger.and.TwoTigerApp;
import com.twotiger.and.activity.HomeActivity;
import com.twotiger.and.util.LogUtil;
import com.twotiger.and.util.PromptManager;
import com.twotiger.and.util.StringUtils;
import com.umeng.analytics.MobclickAgent;

/**
 * 
 * @Description: 
 * 基类 Fragment 
 * @Author douxy
 * @Date 2015年8月10日 下午3:02:53
 */
public abstract class BaseFragment extends Fragment implements OnClickListener {
    protected static HomeActivity mactivity;
    protected SubTitle subTitle;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        // 1.初始化
        mactivity = (HomeActivity) getActivity();
        View loadViewLayout = init(inflater, container);
        return loadViewLayout;
    }

    /**
     * 初始activity方法
     */
    private View init(LayoutInflater inflater, ViewGroup container) {
        View loadViewLayout = loadViewLayout(inflater, container);
        findViewById(loadViewLayout);
        subTitle = new SubTitle(loadViewLayout) {
            @Override
            protected void clickTitle(View v) {
            }

            @Override
            protected void clickRightBtn(View v) {
                clickRightBtnIml();
            }

            @Override
            protected void cickLeftBtn(View v) {
            }
        };

        setListener();
        processLogic();
        return loadViewLayout;
    }
    
    /**
     * 判断用户是否可投新手项目
     * true表示是可投或者未登录，false为不可投
     * @return
     */
    public boolean isNewMan() {
        if (!StringUtils.isEmpty((TwoTigerApp.getInstance().getUser().newerProject))) {
            if ("1".equals(TwoTigerApp.getInstance().getUser().newerProject)) {
                return true;
            } else {
                return false;
            }
        }
        return true; 
    }
    /**
     * 判断用户是否可投万元体验金项目
     * true表示是可投或者未登录，false为不可投
     * @return
     */
    public boolean isNewManForWYTYJ(){
        if(!StringUtils.isEmpty((TwoTigerApp.getInstance().getUser().wytyjProject))){
            if("1".equals(TwoTigerApp.getInstance().getUser().wytyjProject)){
                return true;
            }else{
                return false;
            }
        }
        return true; 
    }

    protected String getBundleString(String key) {
        Bundle bundle = getArguments();
        return bundle == null ? null : bundle.getString(key);
    }

    /**
     * 加载页面layout
     */
    protected abstract View loadViewLayout(LayoutInflater inflater, ViewGroup container);

    /**
     * 加载页面元素
     */
    protected abstract void findViewById(View view);

    /**
     * 点击事件
     */
    public abstract void onClick(View view);

    /**
     * 设置各种事件的监听器
     */
    protected abstract void setListener();

    /**
     * 业务逻辑处理，主要与后端交互
     */
    protected abstract void processLogic();

    /**
     * 是否需要登录
     * @return
     */
    public abstract boolean isNeedLogin();

    public abstract String getTitle();

    /**
     * 模拟后退键
     */
    protected void popBackStack() {
    }

    /**
     * this method will be exe in anytime.
     * Fragment doesn't attached  to Activity
     * so must give an mContext 
     * 
     * @param mContext
     */
    public void rebuildTitleStyle() {
        subTitle.setBgColor(Color.WHITE).setTitle(getTitle()).setLeftName("").setRightName("")
            .setColor(Color.BLACK).hideBtns().hideBottomLine();
        mactivity.tintManager.setTintColor(SystemBarTintManager.DEFAULT_TINT_COLOR);
    }
    
    /**
     * 标题栏右侧按钮的点击事件
     * @return
     */
    public void clickRightBtnIml(){
        
    }

    /**
     * 重新加载数据渲染页面
     */
    public abstract void reLoadData();

    /**
     * 弹出Toast
     *
     * @param text
     */
    public static void showToast(String text) {
        PromptManager.showCustomToast(mactivity, text);
    }

    public void startnewActivity(Intent intent, int enterAnim, int exitAnim, boolean isFinish) {
        mactivity.startActivity(intent);
        mactivity.overridePendingTransition(enterAnim, exitAnim);
        if (isFinish) {
            mactivity.finish();
        }
    }

    /**
     * 第一次加载会执行
     * @see android.support.v4.app.Fragment#onStart()
     */
    @Override
    public void onStart() {
        super.onStart();
        this.rebuildTitleStyle();
        this.reLoadData();
    }
    
    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getName()); //统计页面
    }
    
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getName());
    }

    /**
     * 第二次(包含)以后会执行
     * @see android.support.v4.app.Fragment#onHiddenChanged(boolean)
     */
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        LogUtil.info(BaseFragment.class, getClass().getSimpleName() + " onHiddenChanged : "
                                         + hidden);
        if (!hidden) {
            this.rebuildTitleStyle();
            this.reLoadData();
        }
    }
}
